/*
 * @lc app=leetcode.cn id=637 lang=cpp
 *
 * [637] 二叉树的层平均值
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<double> averageOfLevels(TreeNode* root) {
        vector<double> res;
        if(root == NULL)
            return res;
        
        queue<TreeNode*> q;
        TreeNode* point;
        q.push(root);
        while(!q.empty())
        {
            int size = q.size();
            double Sum = 0;
            for(int i = 0;i < size;i++)
            {
                point = q.front();
                q.pop();
                Sum += point->val;
                if(point->left)
                    q.push(point->left);
                if(point->right)
                    q.push(point->right);
            }
            res.push_back(Sum/size);
        }
        return res;
    }
};
// @lc code=end

